package com.xzbd.springbootmybatiesdm.common.response;

import java.util.HashMap;
import java.util.Map;

public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	public R() {
		put("code", 0);
		put("msg", "操作成功");
	}

	public static R error() {
		return error(1, "操作失败");
	}

	public static R error(String msg) {
		return error(500, msg);
	}

	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("msg", msg);
		return r;
	}

	public static R ok(Map<String, Object> map) {
		R r = new R();
		r.putAll(map);
		return r;
	}

	public static R ok() {
		return new R();
	}

	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
    }
    
    /**
     * 创建一个R,data存在不为null
     * @return
     */
    public static R dataMapBuilder(){
        R r = new R();
        Map<String,Object> dataMap = new HashMap<>();
        r.put("data", dataMap);
        return r;
    }

    /**
     * 默认将 value 放在 data 中
     * @param value 值
     * @return
     */
    public static R dataOk(Object value){
        R r = new R();
        r.put("data", value);
        return r;
    }

    /**
     * 创建一个R,并指定一对键值
     * @param value 值
     * @return
     */
    public static R dataOk(String key,Object value){
        R r = new R();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put(key, value);
        r.put("data", dataMap);
        return r;
    }
    
    /**
     * 分页返回值创建器
     * @param total
     * @param value
     * @return
     */
    public static R pageDataOk(Integer total,Object value){
        R r = new R();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("total", total);
        dataMap.put("list", value);
        r.put("data", dataMap);
        return r;
    }


}